<?php
/**
 * Class that operate on table 'template'. Database Mysql.
 *
 * @version    $Id: TemplateMySqlDAO.class.php 11 2010-04-27 15:38:41Z richardjohnjensen@gmail.com $ 
 * @author: ${author} 
 * @date: 2010-04-08 12:18 
 */
class TemplateMySqlDAO implements TemplateDAO
{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return TemplateMySql 
	 */
	public function load($id) {
		$sql = 'SELECT * FROM template WHERE template = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll() {
		$sql = 'SELECT * FROM template';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn) {
		$sql = 'SELECT * FROM template ORDER BY ' . $orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Delete record from table
	 * @param template primary key
	 */
	public function delete($template) {
		$sql = 'DELETE FROM template WHERE template = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($template);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
	 * Insert record to table
	 *
	 * @param TemplateMySql template
	 */
	public function insert($template) {
		$sql = 'INSERT INTO template (title, document, updated) VALUES (?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($template->title);
		$sqlQuery->set($template->document);
		$sqlQuery->set($template->updated);

		$id = $this->executeInsert($sqlQuery);	
		$template->template = $id;
		return $id;
	}
	
	/**
	 * Update record in table
	 *
	 * @param TemplateMySql template
	 */
	public function update($template) {
		$sql = 'UPDATE template SET title = ?, document = ?, updated = ? WHERE template = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($template->title);
		$sqlQuery->set($template->document);
		$sqlQuery->set($template->updated);

		$sqlQuery->set($template->template);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
	 * Delete all rows
	 */
	public function clean() {
		$sql = 'DELETE FROM template';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByTitle($value) {
		$sql = 'SELECT * FROM template WHERE title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByDocument($value) {
		$sql = 'SELECT * FROM template WHERE document = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdated($value) {
		$sql = 'SELECT * FROM template WHERE updated = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByTitle($value) {
		$sql = 'DELETE FROM template WHERE title = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByDocument($value) {
		$sql = 'DELETE FROM template WHERE document = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdated($value) {
		$sql = 'DELETE FROM template WHERE updated = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return TemplateMySql 
	 */
	protected function readRow($row) {
		$template = new Template();
		
		$template->template = $row['template'];
		$template->title = $row['title'];
		$template->document = $row['document'];
		$template->updated = $row['updated'];

		return $template;
	}
	
	protected function getList($sqlQuery) {
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++) {
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return TemplateMySql 
	 */
	protected function getRow($sqlQuery) {
		$tab = QueryExecutor::execute($sqlQuery);
		if (count($tab) == 0) {
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery) {
		return QueryExecutor::execute($sqlQuery);
	}
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery) {
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery) {
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery) {
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>